//
//  PKCommunityTableViewController.m
//  PianKe
//
//  Created by lanou on 15/12/30.
//  Copyright (c) 2015年 KFC. All rights reserved.
//

#import "PKCommunityTableViewController.h"
#import "PKCommunityPostModel.h"
#import "PKCommunityPostTableViewCell.h"


@interface PKCommunityTableViewController ()

// 数据源
@property (strong, nonatomic) NSMutableArray *listArray;

// 设置初始请求位置
@property (assign, nonatomic) NSInteger requestStart;

@end

@implementation PKCommunityTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
// 初始化数据源数组
    _listArray = [NSMutableArray array];
// 设置网络请求初始加载的位置
    _requestStart = 0;
// 注册cell1
    [self.tableView registerClass:[PKCommunityPostTableViewCell class] forCellReuseIdentifier:@"MyCell1"];
// 解析数据
    [self setUpData];
   
#pragma mark  ----- 上啦加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _requestStart += 10;
        
        [self setUpData];
        
    }];
    
#pragma mark ----- 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _requestStart = 0;
        
        [self setUpData];
        
    }];
    

}

// 刷新方法 : 在主页面点击segment的时候,tableview会调用这个刷新方法,修改是hot还是new
- (void)reloadTableView
{
    
   // 自动进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
}


#pragma mark ----  解析数据
- (void)setUpData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"F7EFB566-54F1-4912-ADFC-7E788F6D4D00";
    parDic[@"limit"] = @"10";
    parDic[@"sort"] = _sortType;
    parDic[@"version"] = @"3.0.6";
    parDic[@"start"] = [NSString stringWithFormat:@"%ld",_requestStart];;
    
    [PKRequestManager requestWithUrlString:_urlString parDic:parDic Method:POST finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = dic[@"data"];
        NSArray *array = dataDic[@"list"];
        
        
        if (_requestStart == 0) {
            
            [_listArray removeAllObjects];
            
        }
        
        for (NSDictionary *oneDic in array) {
                
                PKCommunityPostModel *model = [[PKCommunityPostModel alloc] init];
                [model setValuesForKeysWithDictionary:oneDic];
                [_listArray addObject:model];
        }
        
        
#pragma mark ----- 取消加载
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        [self.tableView reloadData];
        
        
        
    } error:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        PKCommunityPostTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"MyCell1" forIndexPath:indexPath];
        PKCommunityPostModel *model = _listArray[indexPath.row];
        // 展示数据
        cell1.model = model;
        return cell1;
 
}

#pragma mark ------ cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 获取model
    PKCommunityPostModel *model = _listArray[indexPath.row];
    // 因为这个控制器不是受导航栏控制的的子控制器,所以,我们发送通知,让Main控制器去push界面
    NSDictionary *dataDic = @{ @"contentid":model.contentid};
    // 发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"pushWebView" object:self userInfo:dataDic];
    
}


// 返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 让cell 自适应高度
    
    
    // 上边距 + 标题的高度 + 中间间距1 +  content的高度 + 中间间距2 + 评论数(时间)的高度 + 下边距
    // 获取对应的model
        PKCommunityPostModel *model = _listArray[indexPath.row];
        
        if ([model.coverimg isEqualToString:@""]) {
            
            // 说明没有图片 , 需要自适应cell的高度
            CGFloat height = [PKCommunityPostTableViewCell getHeightFromString:model.content];
            
            return 20 * kHeightBase + 50 * kHeightBase + 10 * kHeightBase + height + (10 + 20 + 10)*kHeightBase;
            
            
        } else
            return 165*kHeightBase;
        
        
    
    
    

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
